﻿var shareCodeManager = {};
shareCodeManager.opAddSuccess = "增加成功！";
shareCodeManager.opAddFailed = "增加失败！";
shareCodeManager.opModifySuccess = "修改成功！";
shareCodeManager.opModifyFailed = "修改失败！";
shareCodeManager.opDeleteSuccess = "修改成功！";
shareCodeManager.opDeleteFailed = "修改失败！";
shareCodeManager.opModify = "0";
shareCodeManager.opSave = "1";
shareCodeManager.opClassNameEmpty = "请输出【类别名称】";
shareCodeManager.opCodeNameEmpty = "请输出【项目名称】";
shareCodeManager.updateTextId = "#txt_update_{0}";
shareCodeManager.urlClass = "Class.ashx";
shareCodeManager.urlShareCode = "ShareCode.ashx";

shareCodeManager.getUpdateTextBox = function(id) {
    return jDoc(String.format(this.updateTextId, id));
}
shareCodeManager.getIdBy = function(o) {
    return o.parentNode.getAttribute("data-id");
}
shareCodeManager.getClassList = function() {
    $.post(shareCodeManager.urlClass
        , { t: 1 },
        function(data, textStatus) {
            if (data.success == true) {
                $("#div_List").html(data.message);
            }
        }, "json");
}

shareCodeManager.addClass = function(obj) {
    obj["t"] = 2;
    $.post(shareCodeManager.urlClass,
            obj,
            function(data) {
                if (data.success) {
                    shareCodeManager.getClassList();
                    alert(shareCodeManager.opAddSuccess);
                } else {
                    alert(shareCodeManager.opAddFailed);
                }
            }, "json");
}
shareCodeManager.updateClass = function(id, name) {
    $.post(this.urlClass,
            { id: id, className: name, t: 3 },
            function(data) {
                if (data.success) {
                    shareCodeManager.getUpdateTextBox(id).parentNode.getElementsByTagName("span")[0].innerHTML = name;
                    alert(shareCodeManager.opModifySuccess);
                } else {
                    alert(shareCodeManager.opModifyFailed);
                }
            }, "json");
}
shareCodeManager.modifyClass = function(o) {
    id = this.getIdBy(o);
    if (o.getAttribute("opt") == this.opModify) {
        this.classSetOpText(id, this.opSave);
    } else {
        var updateTextBox = this.getUpdateTextBox(id);
        var value = updateTextBox.value.trim();
        if (value != "") {
            oldValue = updateTextBox.parentNode.getElementsByTagName("span")[0].innerHTML.trim();
            if (value != oldValue) {
                this.updateClass(id, value);
            }
            this.classSetOpText(id, this.opModify);
        } else {
            alert(this.opClassNameEmpty);
            updateTextBox.focus();
        }
    }
}
shareCodeManager.delClass = function(o) {
    if (confirm("确认删除吗？")) {
        $.post(this.urlClass,
            { id: this.getIdBy(o), t: 4 },
            function(data) {
                if (data.success) {
                    shareCodeManager.getClassList();
                    alert(shareCodeManager.opDeleteSuccess);
                } else {
                    alert(shareCodeManager.opDeleteFailed);
                }
            }, "json");
    }
}
shareCodeManager.cancleClass = function(o) {
    this.classSetOpText(this.getIdBy(o), this.opModify);
}

shareCodeManager.classSetOpText = function(id, opt) {
    jDoc("aModify" + id).setAttribute("opt", opt);
    if (opt == this.opSave) {
        jDoc("aModify" + id).innerHTML = "保存";
        var tmp = this.getUpdateTextBox(id);
        tmp.parentNode.getElementsByTagName("span")[0].style.display = "none";
        tmp.style.display = "";
        tmp.focus();
        $("#aCancle" + id).show();
    } else {
        jDoc("aModify" + id).innerHTML = "修改";
        var updateTextBox = this.getUpdateTextBox(id);
        $(updateTextBox).hide();
        updateTextBox.parentNode.getElementsByTagName("span")[0].style.display = "";
        $("#aCancle" + id).hide();
    }
}
//sharecode
shareCodeManager.getList = function(classID, parentId) {
    $.post(this.urlShareCode,
          { classID: classID, parentId: parentId, t: 1 },
          function(data) {
              $("#div_List").html(data.message);
          }
        , "json");
}

shareCodeManager.add = function(value) {
    var classId = jDoc("txt_ClassID").value;
    var parentId = jDoc("txt_ParentId").value;
    $.post(this.urlShareCode,
        { codeName: value, parentId: parentId, classId: classId, t: 2 },
        function(data) {
            if (data.success) {
                shareCodeManager.getList(classId, parentId);
                alert(shareCodeManager.opAddSuccess);
            } else {
                alert(shareCodeManager.opAddFailed);
            }
        }, "json");
}

shareCodeManager.update = function(id, classId, parentId, value) {
    $.post(this.urlShareCode,
        { id: id, codeName: value, t: 3, classId: classId, parentId: parentId },
        function(data) {
            if (data.success) {
                shareCodeManager.getList(classId, parentId);
                alert(shareCodeManager.opModifySuccess);
            } else {
                alert(shareCodeManager.opModifyFailed);
            }
        }, "json");
}
shareCodeManager.modify = function(o) {
    id = this.getIdBy(o);
    if (o.getAttribute("opt") == this.opModify) {
        this.classSetOpText(id, this.opSave);
    } else {
        var updateTextBox = this.getUpdateTextBox(id);
        var value = updateTextBox.value.trim();
        if (value != "") {
            oldValue = updateTextBox.parentNode.getElementsByTagName("span")[0].innerHTML.trim();
            if (value != oldValue) {
                var classId = updateTextBox.getAttribute("data-classid");
                var parentId = updateTextBox.getAttribute("data-pid");
                this.update(id, classId, parentId, value);
            }
            this.classSetOpText(id, this.opModify);
        } else {
            alert(this.opClassNameEmpty);
            updateTextBox.focus();
        }
    }
}

shareCodeManager.del = function(o, classID, parentId) {
    if (confirm("确认删除?")) {
        $.post(this.urlShareCode,
        { id: this.getIdBy(o), t: 4 },
        function(data) {
            if (data.success) {
                shareCodeManager.getList(classID, parentId);
                alert(shareCodeManager.opAddSuccess);
            } else {
                alert(shareCodeManager.opAddFailed);
            }
        }, "json");
    }
}

shareCodeManager.getJSON = function(codeID, name, classID, parentID) {
    var obj = { codeID: codeID, name: name, classID: classID, parentID: parentID };
    return obj;
}
